<template>
  <div v-loading="updateLoading">
    <div class="base_header" style="text-align: left;margin-bottom: 20px">
      <span class="left-title">
        基础信息
      </span>
      <el-button type="success"
                 style="float: right"
                 @click="updateInfo"
      >保存更改</el-button>
    </div>
    <el-form v-model="webInfo"
             label-position="left"
             label-width="120px"
    >
      <el-form-item label="站点标题">
        <el-input v-model="webInfo.title"></el-input>
      </el-form-item>
      <el-form-item label="站点附标题">
        <el-input v-model="webInfo.title2"></el-input>
      </el-form-item>
      <el-form-item label="LOGO">
        <div style="margin-bottom: 20px">
          <el-upload
            class="upload_div"
            :auto-upload="true"
            action=""
            :show-file-list="false"
            :http-request="fileUpload"
            style="display: inline-block"
            :multiple="false">
            <el-button type="">
              <span>点击修改</span>
            </el-button>
          </el-upload>
          <i class="el-icon-loading" v-if="uploading"></i>
        </div>
        <div >
        <el-image :src="webInfo.logo"
                  style="width: 300px;height: 300px"
                  fit="fill"
        ></el-image>
        </div>
      </el-form-item>
      <el-form-item label="是否开启注册">
        <el-switch v-model="webInfo.isRegister"
                   :active-value="1"
                   :inactive-value="0"
                   active-color="#76EE00"
                   inactive-color="#FF0000"
        ></el-switch>
      </el-form-item>
      <el-form-item label="关闭注册原因" v-if="webInfo.isRegister == 0">
        <el-input type="textarea"
                  rows="4"
                  maxlength="200"
                  show-word-limit
                  v-model="webInfo.message">
        </el-input>
      </el-form-item>
      <el-form-item label="新用户角色" >
        <el-select v-model="webInfo.newUserRole">
          <el-option v-for="role in roleList"
                     :label="role.nickname"
                     :value='role.id'
                     :key="role.id"
                     :disabled="role.active === 0">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {mapState} from 'vuex'

export default {
  name: 'BaseInfo',
  data () {
    return {
      uploading: false,
      updateLoading: false,
      roleList: [],
      webInfo: {
        title: '',
        title2: '',
        logo: '',
        isRegister: '',
        message: ''
      }
    }
  },
  mounted () {
    this.getWebInfo()
    this.getAllRole()
  },
  computed: mapState({
    user: 'user'
  }),
  methods: {
    getWebInfo () {
      var _this = this
      this.$axios.post('/api/web/get',
        this.$qs.stringify({
          webId: 1
        })
      ).then(function (response) {
        if (response.data.isSuccess) {
          _this.webInfo = JSON.parse(JSON.stringify(response.data.result))
        }
      })
    },
    updateInfo () {
      this.updateLoading = true
      var _this = this
      if (this.webInfo.isRegister === 1) {
        this.$delete(this.webInfo, 'message')
      }
      this.$axios.post('/api/web/update',
        this.$qs.stringify({
          webData: JSON.stringify(this.webInfo)
        })
      ).then(function (response) {
        _this.updateLoading = false
        if (response.data.isSuccess) {
          _this.$message({
            type: 'success',
            message: '更新成功',
            offset: 50
          })
        }
      })
        .catch(function () {
          _this.updateLoading = false
          _this.$message({
            type: 'error',
            message: '失败，请重试',
            offset: 50
          })
        })
    },
    async fileUpload (file) {
      this.uploading = true
      var _this = this
      var host = this.$store.state.ossData.host
      var response = this.$Utils.getSignature('webImage')
      var requestData = new FormData()
      await response.then(res => {
        requestData = this.$Utils.fileUpload(file.file, res)
      })
      await this.$axios.post(host, requestData, {
        headers: {
          'Content-Type': 'multipart/form-data'
        },
        onUploadProgress: progressEvent => {
          let complete = (progressEvent.loaded / progressEvent.total * 100 | 0)
          file.onProgress({percent: complete})
        }
      })
        .then(function (response) {
          // eslint-disable-next-line eqeqeq
          if (response.status == 200) {
            _this.webInfo.logo = host + '/' + requestData.get('key')
          }
          _this.uploading = false
        })
        .catch(function () {
          _this.uploading = false
        })
    },
    getAllRole () {
      var _this = this
      this.$axios.post('/api/role/getAllRole')
        .then(function (response) {
          var result = response.data
          if (result.isSuccess) {
            _this.roleList = JSON.parse(JSON.stringify(result.result))
            _this.webInfo.newUserRole = parseInt(_this.webInfo.newUserRole) + 1
            _this.webInfo.newUserRole = parseInt(_this.webInfo.newUserRole) - 1
          }
        })
    }
  }
}
</script>

<style scoped>
  .base_header .left-title{
    font-size: 30px;
    font-weight: normal;
  }
</style>
